package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import edu.sdjzu.exam.dto.cheat.MarkCheatingDTO;
import edu.sdjzu.exam.service.cheat.CheatingService;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import edu.sdjzu.exam.common.CommonResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/cheating")
@RequiredArgsConstructor
@SaCheckRole("teacher")
public class CheatingController {

    private final CheatingService cheatingService;

    @PostMapping("/mark")
    public CommonResponse<String> markCheating(@RequestBody MarkCheatingDTO markDTO) {
        try {
            cheatingService.markCheating(markDTO);
            return CommonResponse.success("作弊标记成功");
        } catch (Exception e) {
            return CommonResponse.error("标记作弊失败：" + e.getMessage());
        }
    }

    @PostMapping("/detect-similarity")
    public CommonResponse<String> detectSimilarity(@RequestBody ExamIdDTO examIdDTO) {
        try {
            cheatingService.detectAnswerSimilarity(examIdDTO.getExamId());
            return CommonResponse.success("相似度检测完成");
        } catch (Exception e) {
            return CommonResponse.error("相似度检测失败：" + e.getMessage());
        }
    }

    @Data
    public static class ExamIdDTO {
        @NotNull(message = "试卷ID不能为空")
        private Long examId;
    }
}
